<template>
  <div>
    <a-button v-if="displayType === 'BUTTON'" type="primary" @click="showConfirm">{{ text }}</a-button>
    <a class="gb-display-block" :disabled="disabled" v-if="displayType === 'LINK'" @click="showConfirm">{{ text }}</a>
  </div>
</template>
<script>
import axios from 'axios'

export default {
  props: {
    text: { type: [String] },
    displayType: { type: [String], default: 'BUTTON' },
    callback: { type: [Function] },
    rows: { type: [Array], default: () => { return [] } },
  },
  data() {
    return {
    }
  },
  computed: {
    disabled(){
      if(this.displayType === 'BUTTON'){
        return false
      }
      if(this.rows[0].userStatus === 'L'){
        return false
      }
      return true
    }
  },
  methods: {
    showConfirm() {
      if (this.rows.length == 0) {
        this.$error({
          title: this.$t('error_message'),
          content: this.$t('please_choose_one_record'),
          okText: this.$t('modal_ok'),
        });
        return;
      }
      let _this = this
      let title = ''
      let content = ''
      if(this.displayType == "LINK") {
        title = this.$t('unlock_user')
        content = "请确认解除用户帐号" + this.rows[0].userCode + '（' + this.rows[0].userName +"）的锁定！"
      }
      if(this.displayType == "BUTTON"){
        title = this.$t('confirm_message')
        content = this.$t('please_confirm_unlock_selected_user')
      }
      this.$confirm({
          title: title,
          content: content,
          okText: this.$t('confirm_yes'),
          okType: 'danger',
          cancelText: this.$t('confirm_no'),
          onOk() {
            _this.handleOk()
          },
        });


    },
    handleOk() {
      let selectedIds = this.rows.map(item => item.id);
      axios({
        method: 'post',
        url: '/infra/user/unlock',
        data: selectedIds,
      }).then(() => {
        this.visible = false
        if(this.callback) {
          this.callback();
        }
      });
    },
  }
}
</script>
